package com.frcdi.qms.listener;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.equipment.domain.EquipmentBase;
import com.frcdi.qms.equipment.domain.EquipmentValidation;
import com.frcdi.qms.equipment.service.IEquipmentBaseService;
import com.frcdi.qms.equipment.service.IEquipmentValidationService;
import com.frcdi.qms.masterdata.domain.Change;
import com.frcdi.qms.masterdata.domain.Material;
import com.frcdi.qms.masterdata.domain.Method;
import com.frcdi.qms.masterdata.service.IChangeService;
import com.frcdi.qms.masterdata.service.IMaterialService;
import com.frcdi.qms.masterdata.service.IMethodService;
import com.frcdi.qms.common.CommonConstants;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 功能描述
 *
 * @author: author
 * @date: 2023年11月16日 17:50
 */
@Component
public class MyTaskListener implements ExecutionListener {

    @Resource
    private RepositoryService repositoryService;
    @Autowired
    private IChangeService changeService;
    @Autowired
    private IEquipmentBaseService equipmentService;
    @Autowired
    private IMethodService methodService;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private IEquipmentValidationService validationService;

    @Override
    public void notify(DelegateExecution delegateExecution) {

        delegateExecution.setVariable(CommonConstants.PROCESS_STATUS, CommonConstants.BUSINESS_STATUS_4);
        if (delegateExecution.isEnded()) {
            String processDefinitionId = delegateExecution.getProcessDefinitionId();
            String processType = String.valueOf(delegateExecution.getVariable(CommonConstants.PROCESS_TYPE));
            String processData = String.valueOf(delegateExecution.getVariable(CommonConstants.PROCESS_DATA));
            switch (processType) {
                case UserConstants.PROCESS_MATERIAL:
                    updataMaterial(Long.valueOf(processData));
                    break;
                case UserConstants.PROCESS_METHOD:
                    updataMethod(Long.valueOf(processData));
                    break;
                case UserConstants.PROCESS_EQUIPMENT:
                    updataEquipment(Long.valueOf(processData));
                    break;
                case UserConstants.PROCESS_CHANGE:
                    updataChang(processData);
                    break;
                case UserConstants.PROCESS_EQUIPMENT_VALIDATION:
                    updataEquipmentValidation(Long.valueOf(processData));
                    break;
            }
        }
    }

    private void updataChang(String changNo) {
        Change change = new Change();
        change.setChangeId(Long.valueOf(changNo));
        change.setAuditResult(UserConstants.AUDIT_PASSED);
        change.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        changeService.updateChange(change);
    }

    private void updataEquipmentValidation(Long validationId) {
        EquipmentValidation result = validationService.selectEquipmentValidationByValidationId(validationId);
        // 先修改验证单据状态
        EquipmentValidation equipmentValidation = new EquipmentValidation();
        equipmentValidation.setValidationId(validationId);
        equipmentValidation.setAuditResult(UserConstants.AUDIT_PASSED);
        equipmentValidation.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        validationService.updateEquipmentValidation(equipmentValidation);
        // 根据验证单据修改设备
        Long equipmentId = result.getEquipment();
        String beforeStatus = result.getBeforeStatus();
        String endStatus = result.getEndStatus();
        EquipmentBase equipmentBase = equipmentService.selectEquipmentBaseByEquipmentId(equipmentId);
        if (StringUtils.equals(beforeStatus,equipmentBase.getEquipmentStatus())){
            EquipmentBase up = new EquipmentBase();
            up.setEquipmentId(equipmentBase.getEquipmentId());
            up.setEquipmentStatus(endStatus);
            up.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
            equipmentService.updateEquipmentBase(up);
        }
    }

    private void updataEquipment(Long equipmentId) {
        EquipmentBase equipment = new EquipmentBase();
        equipment.setEquipmentId(equipmentId);
        equipment.setAuditResult(UserConstants.AUDIT_PASSED);
        equipment.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        equipmentService.updateEquipmentBase(equipment);
    }

    private void updataMethod(Long masterdataId) {
        Method method = new Method();
        method.setMethodId(masterdataId);
        method.setAuditResult(UserConstants.AUDIT_PASSED);
        method.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        methodService.updateMethod(method);
    }
    private void updataMaterial(Long masterdataId) {
        Material material = new Material();
        material.setId(masterdataId);
        material.setAuditResult(UserConstants.AUDIT_PASSED);
        material.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        materialService.updateMaterial(material);
    }
}
